import axios from 'axios'
export default {
  namespaced: true,
  state: {
    token: '',
    userInfo: {}
  },
  mutations: {
    updateToken (state, payload) {
      state.token = payload
    },
    updateUserInfo (state, info) {
      state.userInfo = info
    }
  },
  actions: {
    // 进入页面后发送请求，请求是异步的所以放在actions里
    async getUserInfo (ctx) {
      const { data: res } = await axios.get('/my/userinfo', {
        headers: {
          Authorization: ctx.state.token
        }
      })
      // console.log(res)
      if (res.code === 0) {
        ctx.commit('updateUserInfo', res.data)
      }
    }
  },
  getters: {}
}
